package kr.cadis.clms.dao;

import java.io.Reader;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.cadis.clms.model.Holyday;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class HolydayDao {
	Reader reader;
	SqlMapClient sqlMap;
	
	public HolydayDao() {
		try {
			reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		}
		catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	
	public boolean insert(Holyday holyday) throws SQLException {
		sqlMap.insert("Holyday.insert", holyday);
		
		//TODO 검토 필요!!
		return true;
	}
	
	public boolean delete(Date thisDay) throws SQLException {
		
		if(sqlMap.delete("Holyday.delete", thisDay) == 1) { 
			return true;
		} else {
			return false;
		}	
	}
	
	public boolean update(Holyday holyday) throws SQLException {
		if(sqlMap.update("Holyday.update", holyday) == 1) { 
			return true;
		} else {
			return false;
		}	
	}
	
	public Holyday selectByDate(Date thisDay) throws SQLException {
		Holyday holyday = (Holyday) sqlMap.queryForObject("Holyday.selectByDate", thisDay);
		
		return holyday;
	}
	
	public List<Holyday> selectAll() throws SQLException {
		List<Holyday> holydayes = sqlMap.queryForList("Holyday.selectAll");
		
		return holydayes;
	}
	
	public int selectHolydayCountByThisWeek(String startDateOfThisWeek, String finishDateOfThisWeek) throws SQLException {
		Map<String, String> parameterMap = new HashMap<>();
		
		parameterMap.put("startDateOfThisWeek", startDateOfThisWeek);
		parameterMap.put("finishDateOfThisWeek", finishDateOfThisWeek);
		
		Integer holydayCount = (Integer)sqlMap.queryForObject("Holyday.selectHolydayCountByThisWeek", parameterMap);
		
		return holydayCount;
	}
}
